package communication.common.business.chat;

import communication.common.business.base.FcrSocketAddress;
import communication.common.business.base.MsgData;
import communication.common.business.util.MsgDataUtil;
import communication.common.vertx.net.NetManager;
import communication.common.vertx.net.ProtocolEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @author zxl
 */
@Component
@Data
@AllArgsConstructor
@Slf4j
public class ChatServiceImpl implements ChatService{

    private final NetManager netManager;


    @Override
    public void sendChatMsg(ProtocolEnum protocolEnum, String msg, FcrSocketAddress remoteSocketAddress) throws Exception {
        if( protocolEnum == null){
            throw new Exception("protocolEnum is null");
        }

        if(StringUtils.isEmpty(msg)){
            log.warn("[ msg is null , skip send ! ]");
            return;
        }

        if(remoteSocketAddress == null){
            throw new Exception("remoteSocketAddress is null");
        }

        MsgData msgData = MsgDataUtil.createChatMsgData(msg);

        switch (protocolEnum){
            case TCP:
                netManager.sendMsgDataByTcpClient(msgData ,remoteSocketAddress);
                break;
            case UDP:
                netManager.sendMsgDataByUdpServer(msgData,remoteSocketAddress,null);
                break;
            default:
                // nothing
                log.warn("不支持的协议:{}",protocolEnum);
        }
    }

    @Override
    public void handleChatMsg(String msgData, FcrSocketAddress remoteSocketAddress) {
        // todo : nothing
        log.info("接收到聊天信息:{}, sockAddress:{}",msgData,remoteSocketAddress);
    }

}
